<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-07-12 15:43:11 +0200 (Sat, 12 Jul 2008) $
 * $Rev: 1291 $
 * $Author: gavinspearhead $
 * $Id: ajax_admincommands.php 1291 2008-07-12 13:43:11Z gavinspearhead $
 */
define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);

$pathqm = realpath(dirname(__FILE__));

require_once "$pathqm/../functions/ajax_includes.php";
require_once "$pathqm/../functions/pref_functions.php";

$subject = '';

// Process commands:
if (isset($_GET['type']) && isset($_GET['subject']) && isset($_GET['srctype'])) {
	$prefs = load_config($db);
	$type = $_GET['type'];
	$srctype = $_GET['srctype'];
	$subject = $_GET['subject'];

	$items = array();
	switch ($type) {
	case 'browse': // Display menu for the browse page items
		$items[] = new QuickMenuItem('setsearch',$LN['quickmenu_setsearch'],'quickmenu','searchbuttons');
		$items[] = new QuickMenuItem('setshowesi',$LN['quickmenu_setshowesi'],'quickdisplay','showextinfo');
        if (is_seteditor($db, $userID)) {
            $items[] = new QuickMenuItem('seteditesi',$LN['quickmenu_seteditesi'],'quickdisplay','editextinfo');
            // $items[] = new QuickMenuItem('setguessesisafe',$LN['quickmenu_setguessesisafe'],'guessextsetinfosafe','editextinfo');
            $items[] = new QuickMenuItem('setguessesi',$LN['quickmenu_setguessesi'],'guessextsetinfo');
            // For all sets in the basket?
            if (is_array($_SESSION['setdata']) && count($_SESSION['setdata']) > 1)
                $items[] = new QuickMenuItem('setbasketguessesi',$LN['quickmenu_setbasketguessesi'],'guessbasketextsetinfo');
        }

		list($nfo_file_data, $img_file_data, $nzb_file_data) = find_special_file($db, $subject);
		if ($nfo_file_data !== FALSE) 
			$items[] = new QuickMenuItem('setpreviewnfo',$LN['quickmenu_setpreviewnfo'],'nfopreview', $nfo_file_data);
		if ($img_file_data !== FALSE) 
			$items[] = new QuickMenuItem('setpreviewimg',$LN['quickmenu_setpreviewimg'],'imgpreview', $img_file_data);
		if ($nzb_file_data !== FALSE) 
			$items[] = new QuickMenuItem('setpreviewnzb',$LN['quickmenu_setpreviewnzb'],'nzbpreview', $nzb_file_data);
		break;

	case 'setsearch': // Search buttons for a set
		$buttons = get_search_buttons($db, $userID);
		foreach ($buttons as $button)
			$items[] = new QuickMenuItem('search' . htmlentities(utf8_decode($button['name'])), htmlentities(utf8_decode($button['name'])), 'searchbutton', $button);
		break;	

	default:
		$smarty->assign('message', $LN['error_novalidaction']);
		break;
	}
} else {
	die_html('Invalid request.');
}

/* Type can be:
	- quickmenu
	- quickdisplay
	- newpage
   This can be used by the template to determine how to treat a click on the buttom (new menu, display or new page)
   Extra can be the URL (newpage), or a button array (searchbutton).
*/

class QuickMenuItem
{
	public $id;
	public $name;
	public $type;
	public $extra;

	function __construct($id,$name,$type,$extra='')
	{
		$this->id = $id;
		$this->name = $name;
		$this->type = $type;
		$this->extra = $extra;
	}
}


function get_search_buttons(DatabaseConnection $db, $userID)
{
	assert(is_numeric($userID));
	$prefs = load_prefs_clean($db, $userID);

	$rprefs = load_config($db);
	$activebuttons = array();
	$max_buttons = $rprefs['maxbuttons'];
	for ($i = 1; $i <= $max_buttons; $i++) {
		if (isset($prefs["button$i"]) && $prefs["button$i"] != 'none' ) {
			$button[$i] = $prefs["button$i"];
			$qry = "* FROM searchbuttons WHERE \"id\"='{$button[$i]}'";
			$res = $db->select_query($qry);
			if ($res === FALSE)
				continue;
			$row = $res[0];
			$activebuttons[] = $row;
		}
	}
	return $activebuttons;
}


function find_special_file(DatabaseConnection $db, $setID)
{
	$sql = " * FROM setdata WHERE \"ID\" = '$setID'";
	$res = $db->select_query($sql);
	if ($res === FALSE) 
		return array(FALSE, FALSE);
	$search_type = $db->get_pattern_search_command('regexp');

	$groupID = $res[0]['groupID'];
	$sql = " * FROM binaries_$groupID WHERE \"setID\" = '$setID' AND \"subject\" $search_type '.*[.](jpg|gif|png|jpeg)([^.].*)?$'";
	$res = $db->select_query($sql, 1);
	if ($res === FALSE) 
		$rv1 = FALSE;
	else 
		$rv1 =  array('binaryID' => $res[0]['binaryID'], 'groupID' => $groupID);

    $sql = " * FROM binaries_$groupID WHERE \"setID\" = '$setID' AND \"subject\" $search_type '.*[.]nfo([^.].*)?$'";  
	$res = $db->select_query($sql, 1);
	if ($res === FALSE) 
		$rv2 = FALSE;
	else 
		$rv2 =  array('binaryID' => $res[0]['binaryID'], 'groupID' => $groupID);

	$sql = " * FROM binaries_$groupID WHERE \"setID\" = '$setID' AND \"subject\" $search_type '.*[.]nzb([^.].*)?$'";  
	$res = $db->select_query($sql, 1);
	if ($res === FALSE) 
		$rv3 = FALSE;
	else 
		$rv3 =  array('binaryID' => $res[0]['binaryID'], 'groupID' => $groupID);
	return array($rv2, $rv1, $rv3);
}


$smarty->assign('items',	    $items);
$smarty->assign('srctype',      $srctype);     // group or rss
$smarty->assign('subject',      $subject);
$smarty->display('ajax_quickmenu.tpl');


?>
